using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Lib.Utilities
{
    public class FileUtility
    {
        public static void SaveDataToFile(string data, string filePath)
        {
            lock (filePath)
            {
                File.AppendAllText(filePath, data, Encoding.UTF8);
            }
            //StreamWriter sw = File.CreateText(filePath);
            //sw.WriteLine(data);
            //sw.Flush();
            //sw.Close();
        }

        public static void SaveDataToRandomFile(string data, string directory)
        {
            if (directory[directory.Length - 1] != '\\' && directory[directory.Length - 1] != '/')
            {
                directory = directory + "\\";
            }
            Directory.CreateDirectory(directory);
            SaveDataToFile(data, directory + Guid.NewGuid().ToString());
            //StreamWriter sw = File.CreateText(directory + Guid.NewGuid().ToString());

            //sw.Write(data);
            //sw.Flush();
            //sw.Close();
        }

        public static void DeleteFile(string FileName)
        {
            try
            {
                File.Delete(FileName);
            }
            catch (Exception ex)
            {
            }
        }

        public static string GetListFileNames(string directory, bool recursive)
        {
            DirectoryInfo di = new DirectoryInfo(directory);
            string FileNames = "";
            FileInfo[] fi = di.GetFiles();
            foreach (FileInfo f in fi)
                FileNames += "," + f.FullName;
            if(FileNames.StartsWith(","))
                FileNames.Remove(0, 1);
            if (recursive)
            {
                DirectoryInfo[] d = di.GetDirectories();
                foreach (DirectoryInfo dir in d)
                    FileNames = FileNames + "," + GetListFileNames(dir.FullName,recursive);
            }
            return FileNames;
        }

        public static string GetListDirectoryNames(string directory,bool recursive)
        {
            DirectoryInfo di = new DirectoryInfo(directory);
            string DirNames = directory;
            DirectoryInfo[] d = di.GetDirectories();

            foreach (DirectoryInfo dir in d)
                if (recursive)
                    DirNames = DirNames + "," + GetListDirectoryNames(dir.FullName, recursive);
                else
                    DirNames = DirNames + "," + dir.FullName;
            return DirNames;
        }

        public static string GetFileContent(string FileName)
        {
            StreamReader sr = File.OpenText(FileName);
            return sr.ReadToEnd();
        }

        public static string GetFileContent(string FileName, bool _deleteFile)
        {
            StreamReader sr = File.OpenText(FileName);
            string s = sr.ReadToEnd();
            sr.Close();
            if(_deleteFile)
                DeleteFile(FileName);
            return s;
        }
        public static bool checkFileExist(string FileName)
        {
            return File.Exists(FileName);
        }
        public static bool checkFolderExist(string FolderName)
        {
            return Directory.Exists(FolderName);
        }
    }
}
